home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GSTDEV < prev    next >
Text File  |  1991-10-26  |  9KB  |  281 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gstdev.c */
  21. /* Device tracing for Ghostscript library */
  22. #include "gx.h"
  23. #include "gxbitmap.h"            /* for gxdevice.h */
  24. #include "gxfixed.h"            /* for gxmatrix.h */
  25. #include "gxmatrix.h"            /* for gxdevice.h */
  26. #include "gxdevice.h"
  27.  
  28. #ifdef DEBUG
  29.  
  30. /* ------ Tracing 'device' ------*/
  31.  
  32. /* To avoid unpleasant interactions with makedevice, */
  33. /* the tracing 'device' uses an external linked list to keep track of */
  34. /* the real procedures that were replaced in the procedure vector. */
  35.  
  36. typedef struct trace_record_s trace_record;
  37. struct trace_record_s {
  38.     trace_record *next;
  39.     gx_device_procs *tprocs;
  40.     gx_device_procs procs;
  41.     int index;
  42. };
  43.  
  44. private gx_device *trace_cache_device = NULL;
  45. private gx_device_procs *trace_cache_procs;
  46. private trace_record *trace_list = NULL;
  47. private int trace_next_index = 0;
  48.  
  49. #define rprocs\
  50.     (dev == trace_cache_device ? trace_cache_procs :\
  51.      trace_find_procs(dev))
  52.  
  53. /* Procedure structure */
  54. private dev_proc_open_device(trace_open_device);
  55. private dev_proc_get_initial_matrix(trace_get_initial_matrix);
  56. private dev_proc_sync_output(trace_sync_output);
  57. private dev_proc_output_page(trace_output_page);
  58. private dev_proc_close_device(trace_close_device);
  59. private dev_proc_map_rgb_color(trace_map_rgb_color);
  60. private dev_proc_map_color_rgb(trace_map_color_rgb);
  61. private dev_proc_fill_rectangle(trace_fill_rectangle);
  62. private dev_proc_tile_rectangle(trace_tile_rectangle);
  63. private dev_proc_copy_mono(trace_copy_mono);
  64. private dev_proc_copy_color(trace_copy_color);
  65. private dev_proc_draw_line(trace_draw_line);
  66. private dev_proc_fill_trapezoid(trace_fill_trapezoid);
  67. private dev_proc_tile_trapezoid(trace_tile_trapezoid);
  68.  
  69. private gx_device_procs trace_procs = {
  70.     trace_open_device,
  71.     trace_get_initial_matrix,
  72.     trace_sync_output,
  73.     trace_output_page,
  74.     trace_close_device,
  75.     trace_map_rgb_color,
  76.     trace_map_color_rgb,
  77.     trace_fill_rectangle,
  78.     trace_tile_rectangle,
  79.     trace_copy_mono,
  80.     trace_copy_color,
  81.     trace_draw_line,
  82.     trace_fill_trapezoid,
  83.     trace_tile_trapezoid
  84. };
  85.  
  86. /* Find the real procedures for a traced device */
  87. private gx_device_procs *
  88. trace_find_procs(gx_device *tdev)
  89. {    gx_device_procs *tprocs = tdev->procs;
  90.     register trace_record *tp = trace_list;
  91.     while ( tp != NULL )
  92.        {    if ( tp->tprocs == tprocs )
  93.            {    trace_cache_device = tdev;
  94.             return (trace_cache_procs = &tp->procs);
  95.            }
  96.         tp = tp->next;
  97.        }
  98.     lprintf("Traced procedures not found!\n");
  99.     gs_exit(1);
  100. }
  101.  
  102. /* Trace a device. */
  103. gx_device *
  104. gs_trace_device(gx_device *rdev)
  105. {    trace_record *tp;
  106.     if ( rdev->procs->open_device == trace_procs.open_device )
  107.         return rdev;        /* already traced */
  108.     tp = (trace_record *)gs_malloc(1, sizeof(trace_record), 
  109.                        "gs_trace_device");
  110.     if ( tp == 0 ) return 0;
  111.     tp->next = trace_list;
  112.     tp->tprocs = rdev->procs;
  113.     tp->procs = *rdev->procs;
  114.     tp->index = ++trace_next_index;
  115.     trace_list = tp;
  116.     *rdev->procs = trace_procs;
  117.     return rdev;
  118. }
  119.  
  120. /* Utilities */
  121. private int
  122. trace_print_code(int result)
  123. {    if ( result == 0 )
  124.         dprintf(";\n");
  125.     else
  126.         dprintf1(";\t/* = %d */\n", result);
  127.     return result;
  128. }
  129. private void
  130. trace_print_tile(gx_bitmap *tile)
  131. {    int i;
  132.     dprintf1("\t{ static byte data = { 0x%x", tile->data[0]);
  133.     for ( i = 1; i < tile->raster * tile->height; i++ )
  134.         dprintf1(", 0x%x", tile->data[i]);
  135.     dprintf3(" };\n\t  static gx_bitmap tile = { &data, %d, %d, %d };\n",
  136.          tile->raster, tile->width, tile->height);
  137. }
  138.  
  139. /* Procedures */
  140. private int
  141. trace_open_device(gx_device *dev)
  142. {    int result = (*rprocs->open_device)(dev);
  143. if ( gs_debug['v'] )
  144.     dprintf2("[v]\topen_device(0x%lx /*%s*/)", (ulong)dev, dev->dname),
  145.       trace_print_code(result);
  146.     return result;
  147. }
  148. private void
  149. trace_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  150. {    (*rprocs->get_initial_matrix)(dev, pmat);
  151. if ( gs_debug['v'] )
  152.     dprintf6("[v]\tget_initial_matrix(dev) = (%6g, %6g, %6g, %6g, %6g, %6g);\n",
  153.          pmat->xx, pmat->xy, pmat->yx, pmat->yy, pmat->tx, pmat->ty);
  154. }
  155. private int
  156. trace_sync_output(gx_device *dev)
  157. {    int result = (*rprocs->sync_output)(dev);
  158. if ( gs_debug['v'] )
  159.     dprintf("[v]\tsync_output(dev)"),
  160.       trace_print_code(result);
  161.     return result;
  162. }
  163. private int
  164. trace_output_page(gx_device *dev)
  165. {    int result = (*rprocs->output_page)(dev);
  166. if ( gs_debug['v'] )
  167.     dprintf("[v]\toutput_page(dev)"),
  168.       trace_print_code(result);
  169.     return result;
  170. }
  171. private int
  172. trace_close_device(gx_device *dev)
  173. {    int result = (*rprocs->close_device)(dev);
  174. if ( gs_debug['v'] )
  175.     dprintf2("[v]\tclose_device(0x%lx /*%s*/)", (ulong)dev, dev->dname),
  176.       trace_print_code(result);
  177.     return result;
  178. }
  179. private gx_color_index
  180. trace_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  181. {    gx_color_index result = (*rprocs->map_rgb_color)(dev, r, g, b);
  182. if ( gs_debug['v'] )
  183.     dprintf4("[v]\tmap_rgb_color(dev, %u, %u, %u) /*= %ld */;\n",
  184.          r, g, b, (long)result);
  185.     return result;
  186. }
  187. private int
  188. trace_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  189. {    int result = (*rprocs->map_color_rgb)(dev, color, prgb);
  190. if ( gs_debug['v'] )
  191.     dprintf4("[v]\t{ ushort rgb[3]; map_color_rgb(dev, %ld, rgb /* %u, %u, %u */); }",
  192.          (long)color, prgb[0], prgb[1], prgb[2]),
  193.       trace_print_code(result);
  194.     return result;
  195. }
  196. private int
  197. trace_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  198.   gx_color_index color)
  199. {    int result = (*rprocs->fill_rectangle)(dev, x, y, w, h, color);
  200. if ( gs_debug['v'] )
  201.     dprintf5("[v]\tfill_rectangle(dev, %d, %d, %d, %d, %ld)",
  202.          x, y, w, h, (long)color),
  203.       trace_print_code(result);
  204.     return result;
  205. }
  206. private int
  207. trace_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  208.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
  209.   int px, int py)
  210. {    int result = (*rprocs->tile_rectangle)(dev, tile,
  211.         x, y, w, h, zero, one, px, py);
  212. if ( gs_debug['v'] )
  213.    {    trace_print_tile(tile);
  214.     dprintf8("[v]\t  tile_rectangle(dev, &tile, %d, %d, %d, %d, %ld, %ld, %d, %d);\n\t}",
  215.          x, y, w, h, (long)zero, (long)one, px, py);
  216.     trace_print_code(result);
  217.    }
  218.     return result;
  219. }
  220. private int
  221. trace_copy_mono(gx_device *dev, byte *data,
  222.   int dx, int raster, int x, int y, int w, int h,
  223.   gx_color_index zero, gx_color_index one)
  224. {    int result = (*rprocs->copy_mono)(dev, data,
  225.         dx, raster, x, y, w, h, zero, one);
  226. if ( gs_debug['v'] )
  227.     dprintf8("[v]\tcopy_mono(dev, data, %d, %d, %d, %d, %d, %d, %ld, %ld)",
  228.          dx, raster, x, y, w, h, (long)zero, (long)one),
  229.       trace_print_code(result);
  230.     return result;
  231. }
  232. private int
  233. trace_copy_color(gx_device *dev, byte *data,
  234.   int dx, int raster, int x, int y, int w, int h)
  235. {    int result = (*rprocs->copy_color)(dev, data,
  236.         dx, raster, x, y, w, h);
  237. if ( gs_debug['v'] )
  238.     dprintf6("[v]\tcopy_color(dev, data, %d, %d, %d, %d, %d, %d)",
  239.          dx, raster, x, y, w, h),
  240.       trace_print_code(result);
  241.     return result;
  242. }
  243. private int
  244. trace_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  245.   gx_color_index color)
  246. {    int result = (*rprocs->draw_line)(dev, x0, y0, x1, y1, color);
  247. if ( gs_debug['v'] )
  248.     dprintf5("[v]\tdraw_line(dev, %d, %d, %d, %d, %ld)",
  249.          x0, y0, x1, y1, (long)color),
  250.       trace_print_code(result);
  251.     return result;
  252. }
  253. private int
  254. trace_fill_trapezoid(gx_device *dev,
  255.   int x0, int y0, int w0, int x1, int y1, int w1, gx_color_index color)
  256. {    int result = (*rprocs->fill_trapezoid)(dev,
  257.         x0, y0, w0, x1, y1, w1, color);
  258. if ( gs_debug['v'] )
  259.     dprintf7("[v]\tfill_trapezoid(dev, %d, %d, %d, %d, %d, %d, %ld)",
  260.          x0, y0, w0, x1, y1, w1, (long)color),
  261.       trace_print_code(result);
  262.     return result;
  263. }
  264. private int
  265. trace_tile_trapezoid(gx_device *dev, gx_bitmap *tile,
  266.   int x0, int y0, int w0, int x1, int y1, int w1,
  267.   gx_color_index color0, gx_color_index color1, int px, int py)
  268. {    int result = (*rprocs->tile_trapezoid)(dev, tile,
  269.         x0, y0, w0, x1, y1, w1, color0, color1, px, py);
  270. if ( gs_debug['v'] )
  271.    {    trace_print_tile(tile);
  272.     dprintf6("[v]\ttile_trapezoid(dev, %d, %d, %d, %d, %d, %d)",
  273.         x0, y0, w0, x1, y1, w1);
  274.     dprintf4(", %ld, %ld, %d, %d)", (long)color0, (long)color1, px, py);
  275.     trace_print_code(result);
  276.    }
  277.     return result;
  278. }
  279.  
  280. #endif
  281.